package com.kcwl.carrier.infrastructure.repository.persostence;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcwl.carrier.domain.entity.CarrierForeignDriverCondition;
import com.kcwl.carrier.domain.entity.CarrierForeignDriverDetailInfo;
import com.kcwl.carrier.domain.entity.CarrierForeignDriverInfo;
import com.kcwl.carrier.domain.entity.CarrierForeignDriverListInfo;
import com.kcwl.carrier.domain.repository.IForeignDriverRepository;
import com.kcwl.carrier.infrastructure.enums.InviteStatusEnum;
import com.kcwl.carrier.infrastructure.repository.mapper.ForeignDriverMapper;
import com.kcwl.carrier.infrastructure.repository.po.CarrierCooperationMappingPO;
import com.kcwl.ddd.application.constants.YesNoEnum;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.ddd.domain.repository.BaseRepositoryImpl;
import com.kcwl.framework.utils.KcBeanConverter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zxx
 * @date 2022年11月28日 15:02
 * @Description
 */
@Service
public class ForeignDriverRepositoryImpl extends BaseRepositoryImpl<ForeignDriverMapper, CarrierCooperationMappingPO> implements IForeignDriverRepository {

    @Resource
    private ForeignDriverMapper foreignDriverMapper;


    public CarrierCooperationMappingPO queryById(Long id) {
        CarrierCooperationMappingPO carrierCooperationMappingPO = foreignDriverMapper.selectById(id);
        return carrierCooperationMappingPO;
    }

    @Override
    public List<CarrierForeignDriverInfo> getByIds(List<Long> ids) {
        return null;
    }

    @Override
    public int insert(CarrierForeignDriverInfo carrierForeignDriverInfo) {
        //TODO 时间改成日期util生成
        Date date = new Date();
        carrierForeignDriverInfo.setCreateTime(date);
        carrierForeignDriverInfo.setUpdateTime(date);
        CarrierCooperationMappingPO carrierCooperationMappingPO = KcBeanConverter.toBean(carrierForeignDriverInfo, CarrierCooperationMappingPO.class);
        return foreignDriverMapper.insert(carrierCooperationMappingPO);
    }

    @Override
    public int delete(Long id) {
        CarrierCooperationMappingPO carrierCooperationMappingPO = new CarrierCooperationMappingPO();
        carrierCooperationMappingPO.setId(id);
        carrierCooperationMappingPO.setDeleted(YesNoEnum.YEA.getValue());
        return foreignDriverMapper.updateById(carrierCooperationMappingPO);
    }

    @Override
    public int updateById(Long id) {
        CarrierCooperationMappingPO carrierCooperationMappingPO = new CarrierCooperationMappingPO();
        carrierCooperationMappingPO.setId(id);
        carrierCooperationMappingPO.setTransportStatus(YesNoEnum.YEA.getValue());
        return foreignDriverMapper.updateById(carrierCooperationMappingPO);
    }

    @Override
    public KcPage<CarrierForeignDriverListInfo> getForeignDriverList(CarrierForeignDriverCondition condition) {
        IPage<CarrierForeignDriverListInfo> iPage = foreignDriverMapper.getForeignDriverList(new Page<>(condition.getCurPagerNo(), condition.getPageSize()), condition);
        return new KcPage<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }



    @Override
    public CarrierForeignDriverDetailInfo getForeignDriverDetail(CarrierForeignDriverCondition condition) {
        CarrierCooperationMappingPO po = foreignDriverMapper.getForeignDriverDetail(condition);
        return KcBeanConverter.toBean(po, CarrierForeignDriverDetailInfo.class);
    }

    @Override
    public int reAddForeignDriver(Long id) {
        CarrierCooperationMappingPO carrierCooperationMappingPO = new CarrierCooperationMappingPO();
        carrierCooperationMappingPO.setId(id);
        carrierCooperationMappingPO.setInviteStatus(InviteStatusEnum.BE_ACCEPTED.getCode());
        return foreignDriverMapper.updateById(carrierCooperationMappingPO);
    }

    @Override
    public List<Long> selectDriverIdsByCarrierId(Long userId) {
        return foreignDriverMapper.selectDriverIdsByCarrierId(userId);
    }

    @Override
    public int enableForeignDriver(Long id) {
        CarrierCooperationMappingPO carrierCooperationMappingPO = new CarrierCooperationMappingPO();
        carrierCooperationMappingPO.setId(id);
        carrierCooperationMappingPO.setTransportStatus(YesNoEnum.NAY.getValue());
        return foreignDriverMapper.updateById(carrierCooperationMappingPO);
    }

    @Override
    public List<Long> selectVehicleIdsByUserId(Long userId) {
        return foreignDriverMapper.selectVehicleIdsByUserId(userId);
    }

}
